package com.myk.game.gobangchess.utils;

public final class StringUtils {

    /**
     * Return whether the string is null or white space.
     */
    public static boolean isSpace(final String s) {
        if (s == null) return true;
        for (int i = 0, len = s.length(); i < len; ++i) {
            if (!Character.isWhitespace(s.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    public static String assignSize(long fileByte) {
        if (fileByte < 1024) {
            return fileByte + " B";
        }
        if (fileByte < 1024 * 1024) {
            return String.format("%.1f", (double) fileByte / 1024) + " KB";
        }
        if (fileByte < 1024 * 1024 * 1024) {
            return String.format("%.1f", (double) fileByte / (1024 * 1024)) + " MB";
        }
        return String.format("%.1f", (double) fileByte / (1024 * 1024 * 1024)) + " GB";
    }

    /**
     * 判断昵称是否符合要求
     */
    public static boolean isMatch(String newStr) {
        int count = 0;
        for (char c : newStr.toCharArray()) {
            if (isChinese(c)){
                count += 2;
            }
            else if (Character.isLetter(c)){
                count += 1;
            }
            else if (Character.isDigit(c)){
                count += 1;
            }
            else {
                return false;
            }
        }
        return count <= 8;
    }

    /**
     * 判断字符是否为汉字
     */
    public static boolean isChinese(char c) {
        String regex = "[\\u4e00-\\u9fa5]";
        return String.valueOf(c).matches(regex);
    }
}